<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="选择预检单弹窗"
    style="min-width: 1200px"
    draggable
    @close="closeDialog"
  >
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="项目" label-width="70px">
          <el-select
            v-model="searchParam.projectId"
            placeholder="请选择项目"
            clearable
          >
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工序" label-width="70px">
          <el-select
            v-model="searchParam.processId"
            placeholder="请选择工序"
            clearable
          >
            <el-option
              v-for="item in processList"
              :key="item.id"
              :label="item.processName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作类型" label-width="70px">
          <el-select
            v-model="searchParam.opeTypeCode"
            placeholder="请选择操作类型"
            clearable
            @change="changeOpeType"
          >
            <el-option
              v-for="item in useDictionary('CheckOperateType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="事务类型" label-width="70px">
          <el-select
            v-model="searchParam.effectTypeCode"
            placeholder="请选择事务类型"
            clearable
          >
            <el-option
              v-for="item in effectTypeCodeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检验单号" label-width="70px">
          <el-input
            v-model="searchParam.effectCheckCode"
            placeholder="请输入检验单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="预检单号" label-width="70px">
          <el-input
            v-model="searchParam.effectPreCheckCode"
            placeholder="请输入预检单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <div class="formTable">
        <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          border
          @current-change="handleCurrentChange1"
        >
          <el-table-column
            label="检验单号"
            prop="effectCheckCode"
            min-width="150"
            show-overflow-tooltip
          >
            <template #default="scope">
              {{ scope.row.effectCheckCode ?? scope.row.orderNo }}
            </template>
          </el-table-column>
          <el-table-column
            label="事务类型"
            prop="effectTypeCode"
            min-width="100"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              {{ getEffectTypeCode(scope.row) }}
            </template>
          </el-table-column>
          <el-table-column
            label="检验项目名称"
            prop="checkItemName"
            min-width="140"
            show-overflow-tooltip
            align="center"
          />
          <!-- <el-table-column
            label="项目"
            prop="projectName"
            min-width="100"
            show-overflow-tooltip
            align="center"
          /> -->
          <el-table-column
            label="工序"
            prop="processName"
            min-width="100"
            show-overflow-tooltip
            align="center"
          />
          <el-table-column
            label="预检单号"
            prop="effectPreCheckCode"
            min-width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="任务单号"
            prop="taskId"
            min-width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="检验类型"
            prop="checkTypeName"
            min-width="100"
            show-overflow-tooltip
            align="center"
          />
          <el-table-column
            label="检验结果"
            prop="checkResult"
            min-width="100"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <el-tag v-if="scope.row.checkResult === 1" type="success"
                >合格</el-tag
              >
              <el-tag v-if="scope.row.checkResult === 0" type="danger"
                >不合格</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            label="生产时间"
            prop="productTime"
            min-width="150"
            show-overflow-tooltip
            align="center"
          />
          <el-table-column
            label="班次"
            prop="classes"
            min-width="100"
            show-overflow-tooltip
            align="center"
          />
          <el-table-column
            label="操作类型"
            prop="opeTypeCode"
            min-width="120"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              {{ useDictionary("CheckOperateType", scope.row.opeTypeCode) }}
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="100"
            align="center"
          >
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
              >
                查看明细
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :background="true"
          :current-page="pageable.pageNum"
          :page-size="pageable.pageSize"
          :page-sizes="[10, 25, 50, 100]"
          :total="pageable.total"
          layout="total, sizes, prev, pager, next, jumper"
          class="mt-[20px]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
    <qmscheckdoc-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :ruleData="addDialog.ruleData"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import request from "@/utils/request";
defineOptions({
  name: "QMSREPORTMODELRECORDCHECKSELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  brekList: {
    type: Object,
    default: () => {
      return {};
    },
  },
  epCheckPreId: {
    type: String,
    default: () => {
      return "";
    },
  },
});
const multipleTable = ref();
// 请求参数
const requestApi = "/qms/qmscheckdoc/queryByPage";
//获取数据初始化参数
const initParam = {};
const searchParamInit = {
  projectId: "",
  processId: "",
  opeTypeCode: "",
  effectTypeCode: "",
  effectPreCheckCode: "",
  effectCheckCode: "",
  sopTypeCode: "",
};
// 初始化搜索参数数据
// 初始化搜索参数数据
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const dataCallBack = (data: any, parentIndex = "") => {
  return data;
};

// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleCurrentChange,
  handleSizeChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  async (newVal: boolean) => {
    visibleDialog.value = newVal;
    searchParam.value.epCheckPreId = props.epCheckPreId;
    await getProjectList();
    await getProcessList();
    getTableList();
  }
);

// 选择高亮
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  reset();
}
/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择预检单");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const resetHandle = () => {
  Object.assign(searchParam.value, searchParamInit);
  searchParam.value.epCheckPreId = props.epCheckPreId;
  getTableList();
};

const projectList = ref([]);

const getProjectList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprojectinfo/getList",
    method: "post",
    data: {},
  });
  projectList.value = data;
};

const processList = ref([]);

const getProcessList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprocess/getList",
    method: "post",
    data: {},
  });
  processList.value = data;
};

const effectTypeCodeList = ref([]);

const changeOpeType = (val) => {
  if (!searchParam) return;
  searchParam.value.effectTypeCode = "";
  if (val !== null && val !== undefined && val !== "") {
    const filterDictList = useDictionary(val);
    if (filterDictList.length > 0) effectTypeCodeList.value = filterDictList;
    else {
      effectTypeCodeList.value = [
        useDictionary("CheckOperateType").find(
          (item: any) => item.value == val
        ),
      ];
    }
  } else effectTypeCodeList.value = [];
};
const getEffectTypeCode = (row) => {
  const findOperateType = useDictionary("CheckOperateType").find(
    (item: any) => item.value === row.opeTypeCode
  );
  if (findOperateType) {
    const findEffectType = useDictionary(findOperateType.value).find(
      (item: any) => item.value === row.effectTypeCode
    );
    if (findEffectType) {
      return findEffectType.label;
    } else {
      return useDictionary("CheckOperateType", row.effectTypeCode);
    }
  } else return row.effectTypeCode;
};

const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  readvisible: false,
  data: {},
  ruleData: {},
});
/** 打开标准检验预检单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "标准检验检验单详情";
  if (roleObj.checkRuleId !== null && roleObj.checkRuleId !== "") {
    getRule(roleObj.checkRuleId);
  }
  getStandard(roleObj.id);
  addDialog.readvisible = true;
}

const getRule = async (checkRuleId: string) => {
  const { data } = await request({
    url: `/qms/qmscheckrule/get/${checkRuleId}`,
    method: "get",
  });
  addDialog.ruleData = data;
};
// 查询详情
async function getStandard(id: string) {
  request({
    // url: `/qms/qmspointcheckorder/getStandard/${id}`,
    url: `/qms/qmscheckdoc/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
// 查询详情
async function getInfo(id: string) {
  request({
    url: `/qms/qmscheckpre/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>

<style scoped lang="scss">
:deep(.el-table th .cell) {
  color: black;
  font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
